package com.mty.cc.controller;


import com.mty.cc.po.Course;
import com.mty.cc.po.Score;
import com.mty.cc.po.User;
import com.mty.cc.service.CourseService;
import com.mty.cc.service.ScoreService;
import com.mty.cc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/*
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/score")
class ScoreController {


    @Autowired(required=false)
    private ScoreService scoreService;

    @Autowired(required=false)
    private CourseService courseService;

    @Autowired(required=false)
    private UserService userService;

    /*
     * @description: 跳转到首页
     * @author: mty
     */
    @RequestMapping("/scoreIndex")
    public String scoreIndex(HttpServletRequest request,Model model) throws Exception{
        List<Score> scoreList = scoreService.queryByAll();
        int total = scoreList.size();
        HttpSession session = request.getSession();
        String type = session.getAttribute("type").toString();
        model.addAttribute("scoreList", scoreList);
        model.addAttribute("total", total);
        model.addAttribute("type", type);
        return "score/ScoreIndex";
    }


    /*
     * @description: 学生跳转到首页
     * @author: mty
     */
    @RequestMapping("/scoreIndex1")
    public String scoreIndex1(HttpServletRequest request,Model model) throws Exception{
        HttpSession session = request.getSession();
        String type = session.getAttribute("type").toString();
        String username = session.getAttribute("username").toString();
        List<Score> scoreList = scoreService.queryByAllAndStudent(username);
        int total = scoreList.size();
        model.addAttribute("scoreList", scoreList);
        model.addAttribute("total", total);
        model.addAttribute("type", type);
        return "score/ScoreIndex";
    }

    /*
     * @description:进入修改
     * @author: mty
     */
    @RequestMapping("/scoreEdit/{id}")
    public ModelAndView  scoreEdit(@PathVariable("id") String id,Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        Score score = scoreService.queryById(id);
        List<Course> courseList = courseService.queryByAll();
        List<User> userList = userService.queryByAll();
        model.addAttribute("score", score);
        model.addAttribute("courseList", courseList);
        model.addAttribute("userList", userList);
        mv.setViewName("score/ScoreEdit");
        return mv;
    }

    /*
     * @description:修改
     * @author: mty
     */
    @RequestMapping("/scoreEditSubmit")
    public String  scoreEditSubmit(Score score,Model model,HttpServletRequest request) throws Exception{
        try{
            User user = userService.queryById(score.getNo());
            Course course = courseService.queryById(score.getCno());
            score.setNo(user.getUsername());
            score.setRealname(user.getRealname());
            score.setCno(course.getCno());
            score.setCname(course.getCname());
            scoreService.update(score);
            request.setAttribute("msg", "修改成功！");
            return "score/ScoreEdit";
        }catch (Exception e){
            request.setAttribute("msg", "修改失败！");
            return "score/ScoreEdit";
        }
    }

    /*
     * @description:进入添加
     * @author: mty
     */
    @RequestMapping("/scoreAdd")
    public String  scoreAdd(Model model,HttpServletRequest request) throws Exception{
        List<Course> courseList = courseService.queryByAll();
        List<User> userList = userService.queryByAll();
        model.addAttribute("courseList", courseList);
        model.addAttribute("userList", userList);
        return "score/ScoreAdd";
    }

    /*
     * @description:增加
     * @author: mty
     */
    @RequestMapping("/scoreAddSub")
    public String  scoreAddSub(Score score,Model model,HttpServletRequest request) throws Exception{
        try{
            User user = userService.queryById(score.getNo());
            Course course = courseService.queryById(score.getCno());
            score.setNo(user.getUsername());
            score.setRealname(user.getRealname());
            score.setCno(course.getCno());
            score.setCname(course.getCname());
            scoreService.add(score);
            request.setAttribute("msg", "添加成功！");
            return "score/ScoreAdd";
        }catch (Exception e){
            request.setAttribute("msg", "添加失败！");
            return "score/ScoreAdd";
        }
    }

    /*
     * @description:根据ID删除
     * @author: mty
     */
    @RequestMapping("/scoreDel/{id}")
    public ModelAndView scoreDel(@PathVariable("id") String id,HttpServletRequest reuqest, Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        scoreService.deleteById(id);
        mv.setViewName("score/ScoreIndex");
        return mv;
    }



    /*
     * @description:课程的平均分
     * @author: mty
     */
    @RequestMapping("/scoreAvc")
    public ModelAndView scoreAvc(HttpServletRequest reuqest, Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        List<Score> list = scoreService.scoreStatic();
        List<String> data2 = new ArrayList<String>();
        List<String> str = new ArrayList<String>();
        for(int i = 0;i<list.size();i++){
            data2.add(list.get(i).getScore());
            str.add(list.get(i).getCname());
        }
        model.addAttribute("data", data2);
        model.addAttribute("str", str);
        model.addAttribute("size", list.size());
        mv.setViewName("static/index");
        return mv;
    }


    /*
     * @description: 查询不及格学生
     * @author: mty
     */
    @RequestMapping("/scoreMin")
    public String scoreMin(HttpServletRequest request,Model model) throws Exception{
        HttpSession session = request.getSession();
        String type = session.getAttribute("type").toString();
        String username = session.getAttribute("username").toString();
        List<Score> scoreList = scoreService.queryByAll2();
        int total = scoreList.size();
        model.addAttribute("scoreList", scoreList);
        model.addAttribute("total", total);
        model.addAttribute("type", type);
        return "static/index2";
    }

}